Repository navigation
Rebind log handlers to the console when the loading screen closes - #9226
kellyguo11 merged 3 commits into
Conversation
On Windows and macOS the loading screen swaps sys.stdout/sys.stderr for a temporary spool. Logging handlers created during startup captured the spool, so every record logged after it closed printed a "--- Logging error ---" traceback with "I/O operation on closed file". Point those handlers back at the real streams when the console is restored.
[Medium impact] The PR appears safe to merge. SummaryRestores logging output after the loading screen closes on Windows and macOS.
Tests were inspected, not run. Reviews (1) · Last reviewed commit: "Rebind log handlers to the console when ..." · Reviewed by Greptile |
There was a problem hiding this comment.
Isaac Lab Review Bot
Needs attention: Compatibility and deprecation (1), Implementation (2), Style consistency (4), Test quality (2). Other reviewed categories look good.
- PR description: The title, platform scope, stderr-routing rationale, and reported regression match the diff; replace “every logging stream handler” with “handlers attached to registered loggers”; no performance claim requires benchmarking.
- Design and architecture: Architecture looks good.
- Scope and over-engineering: No over-engineering issues.
- API: API changes look good.
- Compatibility and deprecation: Breaking changes: redirected stderr no longer supports iteration or context-manager operations because getattr does not forward special methods; explicitly forward the former file operations.
- Implementation: Dictionary membership hashes unrelated handler streams and can abort restoration with TypeError; compare streams by identity instead.
- Style consistency: The added declarations violate the prescribed private-function/class order; move _StreamAlias below the private functions.
- Test quality: The distinct post-startup logging regression lacks kitless CI selection; add an explicit pytest.mark.kitless marker.
Significant concerns. Posted 9 inline findings.
Automated review; human maintainers own approval decisions.
| elif self._saved_streams is not None: | ||
| sys.stdout.flush() | ||
| sys.stderr.flush() | ||
| spooled_streams = (sys.stdout, sys.stderr) |
There was a problem hiding this comment.
🔴 Critical · Implementation — Match the installed spool streams
If stdout or stderr is replaced again during startup, these current globals are not the streams earlier handlers captured. Those handlers remain bound to the closed spool. Retain and match the installed streams.
| loggers = [logging.getLogger(), *logging.Logger.manager.loggerDict.values()] | ||
| for logger in loggers: | ||
| for handler in getattr(logger, "handlers", ()): | ||
| if isinstance(handler, logging.StreamHandler) and handler.stream in replacements: |
There was a problem hiding this comment.
🔴 Critical · Implementation — Compare handler streams by identity
A StreamHandler can hold an unhashable stream. Dictionary membership then raises TypeError on an unrelated handler, interrupting _restore before spool cleanup. Compare stream objects by identity instead.
| sys.stdout = self._spool | ||
| sys.stderr = self._spool | ||
| # a distinct object lets _restore tell handlers bound to stderr from those bound to stdout | ||
| sys.stderr = _StreamAlias(self._spool) |
There was a problem hiding this comment.
🟡 Warning · Compatibility — Preserve stderr's file protocol
Special methods do not pass through getattr. Iterating redirected sys.stderr or entering it as a context manager now raises TypeError, unlike the former file object. Forward those operations on the alias.
| return spooled | ||
|
|
||
|
|
||
| class _StreamAlias: |
There was a problem hiding this comment.
🟡 Warning · Style Consistency — Order private functions before classes
_StreamAlias precedes _rebind_log_handlers and the remaining private functions, contrary to the prescribed file order. Move the class below the private functions.
| class _StreamAlias: | ||
| """Forward every attribute to *target* while being a distinct object from it.""" | ||
|
|
||
| def __init__(self, target: IO[str]): |
There was a problem hiding this comment.
🟡 Warning · Style Consistency — Annotate no-value signatures
The new constructor and regression test omit -> None despite the contribution rule for no-value functions. Annotate both signatures.
|
|
||
|
|
||
| def _rebind_log_handlers(replacements: dict[Any, IO[str]]) -> None: | ||
| """Point every logging stream handler writing to a key of *replacements* at the mapped stream.""" |
There was a problem hiding this comment.
🟡 Warning · Style Consistency — Remove signature-paraphrasing docstring
The helper docstring merely restates its mapping argument and operation, adding no constraint or intent. Remove it or document the non-obvious ownership boundary.
| """Point every logging stream handler writing to a key of *replacements* at the mapped stream.""" | ||
| loggers = [logging.getLogger(), *logging.Logger.manager.loggerDict.values()] | ||
| for logger in loggers: | ||
| for handler in getattr(logger, "handlers", ()): |
There was a problem hiding this comment.
🟡 Warning · Style Consistency — Filter logger placeholders explicitly
loggerDict also contains PlaceHolder objects. getattr with an empty default obscures that distinction and violates the direct-access rule. Filter for logging.Logger, then read logger.handlers.
| assert loading_screen.sys.stderr is original_stderr | ||
|
|
||
|
|
||
| def test_restore_rebinds_log_handlers_created_during_stream_swapping(monkeypatch: pytest.MonkeyPatch): |
There was a problem hiding this comment.
🟡 Warning · Test Quality — Route regression through kitless CI
This regression needs no Isaac Sim, but the file contains no pytest.mark.kitless text for CI selection. Mark the test, or the merged lifecycle test, explicitly.
|
|
||
|
|
||
| def test_restore_rebinds_log_handlers_created_during_stream_swapping(monkeypatch: pytest.MonkeyPatch): | ||
| original_stdout = _OpenStringIO() |
There was a problem hiding this comment.
🟡 Warning · Test Quality — Extend the existing lifecycle test
The preceding test already constructs both streams and runs redirect/restore. Add the handler-output assertions there instead of maintaining a second copy of that lifecycle setup.
…dlers Keep the installed spool streams instead of reading sys.stdout/sys.stderr at restore time, compare handler streams by identity so an unhashable stream cannot abort the restore, and skip logger placeholders explicitly. Fold the regression into the existing stream-swapping test and mark it kitless.
|
run-ci |
|
run-ci |
) # Description On Windows and macOS, the loading screen hides startup output by replacing `sys.stdout` / `sys.stderr` with a temporary spool file (Linux redirects file descriptors instead and is unaffected). Any `logging.StreamHandler` created while the screen is up — e.g. by `ensure_console_handlers` or `configure_logging` — binds to that spool. Once startup finishes the spool is closed, so every later record fails with `ValueError: I/O operation on closed file` and Python prints a `--- Logging error ---` traceback. The run itself is unaffected. Seen running `examples/sensors/foveated_camera.py` on Windows, where the first record after startup was the Newton site view's `"Fabric runtime unavailable; Newton site poses will not be mirrored to prims."` INFO message. Fix: when `LoadingScreen._restore` puts the original streams back, it now repoints every logging stream handler that still writes to the spool at the matching real stream. `sys.stderr` is a thin alias of the spool during startup so handlers created on stderr go back to stderr rather than stdout. ## Type of change - Bug fix (non-breaking change which fixes an issue) ## Release backport - [x] <!-- backport-active-release --> Backport this pull request to the active release branch after it merges into `main` ## Checklist - [x] I have read and understood the [contribution guidelines](https://isaac-sim.github.io/IsaacLab/main/source/refs/contributing.html) - [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with `uv run isaaclab --format` (ruff check / format) - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have validated the changed behavior with focused coverage and applied the [test-audit criteria](https://github.com/isaac-sim/IsaacLab/blob/HEAD/skills/developer/test-audit/SKILL.md) to any test changes — added `test_restore_rebinds_log_handlers_created_during_stream_swapping`, which fails before the fix and passes after - [x] I have added a changelog fragment under `source/<pkg>/changelog.d/` for every touched package - [x] I have added my name to the `CONTRIBUTORS.md` or my name already exists there --------- Co-authored-by: Antoine RICHARD <antoiner@nvidia.com> (cherry picked from commit 19cff01)
|
Backported to |
Description
On Windows and macOS, the loading screen hides startup output by replacing
sys.stdout/sys.stderrwith a temporary spool file (Linux redirects file descriptors instead and is unaffected). Anylogging.StreamHandlercreated while the screen is up — e.g. byensure_console_handlersorconfigure_logging— binds to that spool. Once startup finishes the spool is closed, so every later record fails withValueError: I/O operation on closed fileand Python prints a--- Logging error ---traceback. The run itself is unaffected.Seen running
examples/sensors/foveated_camera.pyon Windows, where the first record after startup was the Newton site view's"Fabric runtime unavailable; Newton site poses will not be mirrored to prims."INFO message.Fix: when
LoadingScreen._restoreputs the original streams back, it now repoints every logging stream handler that still writes to the spool at the matching real stream.sys.stderris a thin alias of the spool during startup so handlers created on stderr go back to stderr rather than stdout.Type of change
Release backport
mainChecklist
pre-commitchecks withuv run isaaclab --format(ruff check / format)test_restore_rebinds_log_handlers_created_during_stream_swapping, which fails before the fix and passes aftersource/<pkg>/changelog.d/for every touched packageCONTRIBUTORS.mdor my name already exists there